package com.hezhi.project.module.abnormal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hezhi.common.utils.StringUtils;
import com.hezhi.project.module.abnormal.mapper.AbnormalMapper;
import com.hezhi.project.module.abnormal.domain.Abnormal;
import com.hezhi.project.module.abnormal.service.IAbnormalService;
import com.hezhi.project.weixin.utils.TemplateUtils;
import com.alibaba.fastjson.JSONObject;
import com.hezhi.common.support.Convert;

/**
 * 进出异常记录 服务层实现
 * 
 * @author hezhi
 * @date 2018-08-10
 */
@Service
public class AbnormalServiceImpl implements IAbnormalService 
{
	@Autowired
	private AbnormalMapper abnormalMapper;

	/**
     * 查询进出异常记录信息
     * 
     * @param abnormalId 进出异常记录ID
     * @return 进出异常记录信息
     */
    @Override
	public Abnormal selectAbnormalById(Integer abnormalId)
	{
	    return abnormalMapper.selectAbnormalById(abnormalId);
	}
	
	/**
     * 查询进出异常记录列表
     * 
     * @param abnormal 进出异常记录信息
     * @return 进出异常记录集合
     */
	@Override
	public List<Abnormal> selectAbnormalList(Abnormal abnormal)
	{
	    return abnormalMapper.selectAbnormalList(abnormal);
	}
	
    /**
     * 新增进出异常记录
     * 
     * @param abnormal 进出异常记录信息
     * @return 结果
     */
	@Override
	public int insertAbnormal(Abnormal abnormal)
	{
	    return abnormalMapper.insertAbnormal(abnormal);
	}
	
	/**
     * 修改进出异常记录
     * 
     * @param abnormal 进出异常记录信息
     * @return 结果
     */
	@Override
	public int updateAbnormal(Abnormal abnormal)
	{
	    return abnormalMapper.updateAbnormal(abnormal);
	}
	
	/**
     * 保存进出异常记录
     * 
     * @param abnormal 进出异常记录信息
     * @return 结果
     */
	@Override
	public int saveAbnormal(Abnormal abnormal)
	{
	    Integer abnormalId = abnormal.getAbnormalId();
		if (StringUtils.isNotNull(abnormalId))
        {
		    abnormalMapper.updateAbnormal(abnormal);
		    return abnormalId;
		}
		else
        {
		    return abnormalMapper.insertAbnormal(abnormal);
		}
	}
	
	/**
     * 删除进出异常记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAbnormalByIds(String ids)
	{
		return abnormalMapper.deleteAbnormalByIds(Convert.toIntArray(ids));
	}

	/**
	 * 查找学生的进出记录
	 */
	@Override
	public List<Map<String, Object>> selectAbnormalStudentList(Abnormal abnormal) {
		return abnormalMapper.selectAbnormalStudentList(abnormal);
	}

	/**
	 * 新增当天没有入校的学生异常记录
	 */
	@Override
	public void abnormalByInSchool() {
		abnormalMapper.abnormalByInSchool();
		sendMessage("1");
	}

	/**
	 * 新增当天没有出校的异常记录
	 */
	@Override
	public void abnormalByOutSchool() {
		abnormalMapper.abnormalByOutSchool();
		sendMessage("6");
	}
	
	/**
	 * 发送微信模板消息
	 * @param inOutFlag
	 */
	public void sendMessage(String status){
		List<Map<String, Object>> list = abnormalMapper.findAbnormalByStatus(status);
		for(Map<String,Object> m:list){
			if(m.get("open_id") != null){
				Map<String, Object> map = new HashMap<>();
				Map<String, Object> map2 = new HashMap<>();
				map.put("first", "1".equals(status)?"家长您好，您的孩子未入校园":"家长您好，您的孩子未出校园");
				map.put("keyword1", JSONObject.parseObject("{\"value\":\""+map.get("name")+"\",\"color\":\"#173177\"}"));
				map2.put("first", "1".equals(status)?"老师您好，有学生未入校园":"家长您好，有学生未出校园");
				map2.put("keyword1", JSONObject.parseObject("{\"value\":\""+map.get("name")+"\",\"color\":\"#173177\"}"));
				if("1".equals(status)){
					map.put("keyword2", JSONObject.parseObject("{\"value\":\"进校\",\"color\":\"#173177\"}"));
					map2.put("keyword2", JSONObject.parseObject("{\"value\":\"进校\",\"color\":\"#173177\"}"));
				}else{
					map.put("keyword2", JSONObject.parseObject("{\"value\":\"出校\",\"color\":\"#173177\"}"));
					map2.put("keyword2", JSONObject.parseObject("{\"value\":\"出校\",\"color\":\"#173177\"}"));
				}
				map.put("keyword3", JSONObject.parseObject("{\"value\":\""+map.get("in_out_date").toString()+"\",\"color\":\"#173177\"}"));
				map.put("remark", JSONObject.parseObject("{\"value\":\"点击详情可查看现场图文信息。\",\"color\":\"#173177\"}"));
				map2.put("keyword3", JSONObject.parseObject("{\"value\":\""+map.get("in_out_date").toString()+"\",\"color\":\"#173177\"}"));
				map2.put("remark", JSONObject.parseObject("{\"value\":\"点击详情可查看现场图文信息。\",\"color\":\"#173177\"}"));
				TemplateUtils.send(m.get("open_id").toString(), TemplateUtils.INOUTSCHOOL, "", map);
				TemplateUtils.send(m.get("teacher_open_id").toString(), TemplateUtils.INOUTSCHOOL, "", map2);
			}
		}
	}
	
}
